package fr.futsal.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import fr.futsal.domaine.Equipe;
import fr.futsal.servlet.EquipeServlet;

public class EquipeDAOImpl implements EquipeDAO {
	private Connection connectionDataSource;

	public EquipeDAOImpl(Connection connectionDataSource) {
		super();
		this.connectionDataSource = connectionDataSource;
	}

	@Override
	public List<Equipe> collecteEquipe() {
		// TODO Auto-generated method stub
		List<Equipe> ListeEquipe = new ArrayList<Equipe>();

		try {
			// connection
			Statement statement = connectionDataSource.createStatement();

			// resultat
			ResultSet resultat = statement
					.executeQuery("select nom,logo from EQUIPE");

			while (resultat.next()) {
				Equipe equipe = new Equipe();

				String nom = resultat.getString("nom");
				String logo = resultat.getString("logo");

				equipe.setNameEquipe(nom);
				equipe.setAdresse(logo);

				ListeEquipe.add(equipe);

			}

			return ListeEquipe;
		} catch (SQLException exception) {
			return Collections.emptyList();
		}
	}

	@Override
	public void enregistrerEquipe(Equipe equipe) {
		
		try {
			// connection
			Statement statement = connectionDataSource.createStatement();
			
			// resultat
			statement.executeUpdate("insert into EQUIPE(nom,logo) values ('" + equipe.getNameEquipe()+ "','" + equipe.getAdresse()+"')");
				
		}catch (SQLException exception) {
		}
		
	}

	public void supprimerEquipe(String nomEquipe) {
		try {
			// connection
			Statement statement = connectionDataSource.createStatement();
			
			// resultat
			statement.executeUpdate("delete from EQUIPE where nom='"+nomEquipe+"'");
				
		}catch (SQLException exception) {
			exception.printStackTrace();
		}
	}
}
